
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.test.testcases &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-test-testcases">
            
  <h1>Source code for django.test.testcases</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">difflib</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">posixpath</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">unittest</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">Counter</span>
<span class="kn">from</span> <span class="nn">contextlib</span> <span class="k">import</span> <span class="n">contextmanager</span>
<span class="kn">from</span> <span class="nn">copy</span> <span class="k">import</span> <span class="n">copy</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">wraps</span>
<span class="kn">from</span> <span class="nn">unittest.util</span> <span class="k">import</span> <span class="n">safe_repr</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="k">import</span> <span class="n">mail</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">ValidationError</span>
<span class="kn">from</span> <span class="nn">django.core.files</span> <span class="k">import</span> <span class="n">locks</span>
<span class="kn">from</span> <span class="nn">django.core.handlers.wsgi</span> <span class="k">import</span> <span class="n">WSGIHandler</span><span class="p">,</span> <span class="n">get_path_info</span>
<span class="kn">from</span> <span class="nn">django.core.management</span> <span class="k">import</span> <span class="n">call_command</span>
<span class="kn">from</span> <span class="nn">django.core.management.color</span> <span class="k">import</span> <span class="n">no_style</span>
<span class="kn">from</span> <span class="nn">django.core.management.sql</span> <span class="k">import</span> <span class="n">emit_post_migrate_signal</span>
<span class="kn">from</span> <span class="nn">django.core.servers.basehttp</span> <span class="k">import</span> <span class="n">WSGIRequestHandler</span><span class="p">,</span> <span class="n">WSGIServer</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">DEFAULT_DB_ALIAS</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="n">connections</span><span class="p">,</span> <span class="n">transaction</span>
<span class="kn">from</span> <span class="nn">django.forms.fields</span> <span class="k">import</span> <span class="n">CharField</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">QueryDict</span>
<span class="kn">from</span> <span class="nn">django.http.request</span> <span class="k">import</span> <span class="n">split_domain_port</span><span class="p">,</span> <span class="n">validate_host</span>
<span class="kn">from</span> <span class="nn">django.test.client</span> <span class="k">import</span> <span class="n">Client</span>
<span class="kn">from</span> <span class="nn">django.test.html</span> <span class="k">import</span> <span class="n">HTMLParseError</span><span class="p">,</span> <span class="n">parse_html</span>
<span class="kn">from</span> <span class="nn">django.test.signals</span> <span class="k">import</span> <span class="n">setting_changed</span><span class="p">,</span> <span class="n">template_rendered</span>
<span class="kn">from</span> <span class="nn">django.test.utils</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">CaptureQueriesContext</span><span class="p">,</span> <span class="n">ContextList</span><span class="p">,</span> <span class="n">compare_xml</span><span class="p">,</span> <span class="n">modify_settings</span><span class="p">,</span>
    <span class="n">override_settings</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.decorators</span> <span class="k">import</span> <span class="n">classproperty</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="k">import</span> <span class="n">RemovedInDjango20Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves.urllib.parse</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">unquote</span><span class="p">,</span> <span class="n">urljoin</span><span class="p">,</span> <span class="n">urlparse</span><span class="p">,</span> <span class="n">urlsplit</span><span class="p">,</span> <span class="n">urlunsplit</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves.urllib.request</span> <span class="k">import</span> <span class="n">url2pathname</span>
<span class="kn">from</span> <span class="nn">django.views.static</span> <span class="k">import</span> <span class="n">serve</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;TestCase&#39;</span><span class="p">,</span> <span class="s1">&#39;TransactionTestCase&#39;</span><span class="p">,</span>
           <span class="s1">&#39;SimpleTestCase&#39;</span><span class="p">,</span> <span class="s1">&#39;skipIfDBFeature&#39;</span><span class="p">,</span> <span class="s1">&#39;skipUnlessDBFeature&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">to_list</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Puts value into a list if it&#39;s not already one.</span>
<span class="sd">    Returns an empty list if value is None.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">value</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">elif</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">value</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">value</span>


<span class="k">def</span> <span class="nf">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html</span><span class="p">,</span> <span class="n">user_msg</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">dom</span> <span class="o">=</span> <span class="n">parse_html</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">HTMLParseError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="n">standardMsg</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="se">\n</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_formatMessage</span><span class="p">(</span><span class="n">user_msg</span><span class="p">,</span> <span class="n">standardMsg</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">dom</span>


<span class="k">class</span> <span class="nc">_AssertNumQueriesContext</span><span class="p">(</span><span class="n">CaptureQueriesContext</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test_case</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_case</span> <span class="o">=</span> <span class="n">test_case</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">=</span> <span class="n">num</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">_AssertNumQueriesContext</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">connection</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">_AssertNumQueriesContext</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__exit__</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">exc_type</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">executed</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_case</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
            <span class="n">executed</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num</span><span class="p">,</span>
            <span class="s2">&quot;</span><span class="si">%d</span><span class="s2"> queries executed, </span><span class="si">%d</span><span class="s2"> expected</span><span class="se">\n</span><span class="s2">Captured queries were:</span><span class="se">\n</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">executed</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num</span><span class="p">,</span>
                <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
                    <span class="n">query</span><span class="p">[</span><span class="s1">&#39;sql&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">query</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">captured_queries</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>


<span class="k">class</span> <span class="nc">_AssertTemplateUsedContext</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test_case</span><span class="p">,</span> <span class="n">template_name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_case</span> <span class="o">=</span> <span class="n">test_case</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span> <span class="o">=</span> <span class="n">template_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rendered_templates</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rendered_template_names</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">context</span> <span class="o">=</span> <span class="n">ContextList</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">on_template_render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">signal</span><span class="p">,</span> <span class="n">template</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rendered_templates</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">template</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rendered_template_names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">template</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">copy</span><span class="p">(</span><span class="n">context</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rendered_template_names</span>

    <span class="k">def</span> <span class="nf">message</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> was not rendered.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">template_rendered</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">on_template_render</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="n">template_rendered</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">on_template_render</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">exc_type</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">test</span><span class="p">():</span>
            <span class="n">message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rendered_templates</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">message</span> <span class="o">+=</span> <span class="s1">&#39; No template was rendered.&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">message</span> <span class="o">+=</span> <span class="s1">&#39; Following templates were rendered: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rendered_template_names</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">test_case</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_AssertTemplateNotUsedContext</span><span class="p">(</span><span class="n">_AssertTemplateUsedContext</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rendered_template_names</span>

    <span class="k">def</span> <span class="nf">message</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> was rendered.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span>


<span class="k">class</span> <span class="nc">_CursorFailure</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls_name</span><span class="p">,</span> <span class="n">wrapped</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span> <span class="o">=</span> <span class="n">cls_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapped</span> <span class="o">=</span> <span class="n">wrapped</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span>
            <span class="s2">&quot;Database queries aren&#39;t allowed in SimpleTestCase. &quot;</span>
            <span class="s2">&quot;Either use TestCase or TransactionTestCase to ensure proper test isolation or &quot;</span>
            <span class="s2">&quot;set </span><span class="si">%s</span><span class="s2">.allow_database_queries to True to silence this failure.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span>
        <span class="p">)</span>


<div class="viewcode-block" id="SimpleTestCase"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase">[docs]</a><span class="k">class</span> <span class="nc">SimpleTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>

    <span class="c1"># The class we&#39;ll use for the test client self.client.</span>
    <span class="c1"># Can be overridden in derived classes.</span>
    <span class="n">client_class</span> <span class="o">=</span> <span class="n">Client</span>
    <span class="n">_overridden_settings</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">_modified_settings</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="c1"># Tests shouldn&#39;t be allowed to query the database since</span>
    <span class="c1"># this base class doesn&#39;t enforce any isolation.</span>
    <span class="n">allow_database_queries</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">setUpClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SimpleTestCase</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">setUpClass</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_overridden_settings</span><span class="p">:</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_cls_overridden_context</span> <span class="o">=</span> <span class="n">override_settings</span><span class="p">(</span><span class="o">**</span><span class="bp">cls</span><span class="o">.</span><span class="n">_overridden_settings</span><span class="p">)</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_cls_overridden_context</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_modified_settings</span><span class="p">:</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_cls_modified_context</span> <span class="o">=</span> <span class="n">modify_settings</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_modified_settings</span><span class="p">)</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_cls_modified_context</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">allow_database_queries</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">alias</span> <span class="ow">in</span> <span class="n">connections</span><span class="p">:</span>
                <span class="n">connection</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">cursor</span> <span class="o">=</span> <span class="n">_CursorFailure</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">)</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">chunked_cursor</span> <span class="o">=</span> <span class="n">_CursorFailure</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">connection</span><span class="o">.</span><span class="n">chunked_cursor</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">tearDownClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">allow_database_queries</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">alias</span> <span class="ow">in</span> <span class="n">connections</span><span class="p">:</span>
                <span class="n">connection</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">cursor</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="o">.</span><span class="n">wrapped</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">chunked_cursor</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">chunked_cursor</span><span class="o">.</span><span class="n">wrapped</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;_cls_modified_context&#39;</span><span class="p">):</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_cls_modified_context</span><span class="o">.</span><span class="n">disable</span><span class="p">()</span>
            <span class="nb">delattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;_cls_modified_context&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;_cls_overridden_context&#39;</span><span class="p">):</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_cls_overridden_context</span><span class="o">.</span><span class="n">disable</span><span class="p">()</span>
            <span class="nb">delattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;_cls_overridden_context&#39;</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SimpleTestCase</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">tearDownClass</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Wrapper around default __call__ method to perform common Django test</span>
<span class="sd">        set up. This means that user-defined Test Cases aren&#39;t required to</span>
<span class="sd">        include a call to super().setUp().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">testMethod</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_testMethodName</span><span class="p">)</span>
        <span class="n">skipped</span> <span class="o">=</span> <span class="p">(</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="s2">&quot;__unittest_skip__&quot;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="ow">or</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="n">testMethod</span><span class="p">,</span> <span class="s2">&quot;__unittest_skip__&quot;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">skipped</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_pre_setup</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="n">result</span><span class="o">.</span><span class="n">addError</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">())</span>
                <span class="k">return</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SimpleTestCase</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__call__</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">skipped</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_post_teardown</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="n">result</span><span class="o">.</span><span class="n">addError</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">())</span>
                <span class="k">return</span>

    <span class="k">def</span> <span class="nf">_pre_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Performs any pre-test setup. This includes:</span>

<span class="sd">        * Creating a test client.</span>
<span class="sd">        * Clearing the mail test outbox.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client_class</span><span class="p">()</span>
        <span class="n">mail</span><span class="o">.</span><span class="n">outbox</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_post_teardown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Perform any post-test things.&quot;&quot;&quot;</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="SimpleTestCase.settings"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.settings">[docs]</a>    <span class="k">def</span> <span class="nf">settings</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A context manager that temporarily sets a setting and reverts to the original value when exiting the context.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">override_settings</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.modify_settings"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.modify_settings">[docs]</a>    <span class="k">def</span> <span class="nf">modify_settings</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A context manager that temporarily applies changes a list setting and</span>
<span class="sd">        reverts back to the original value when exiting the context.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">modify_settings</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertRedirects"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertRedirects">[docs]</a>    <span class="k">def</span> <span class="nf">assertRedirects</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">expected_url</span><span class="p">,</span> <span class="n">status_code</span><span class="o">=</span><span class="mi">302</span><span class="p">,</span>
                        <span class="n">target_status_code</span><span class="o">=</span><span class="mi">200</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
                        <span class="n">fetch_redirect_response</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Asserts that a response redirected to a specific URL, and that the</span>
<span class="sd">        redirect URL can be loaded.</span>

<span class="sd">        Note that assertRedirects won&#39;t work for external links since it uses</span>
<span class="sd">        TestClient to do a request (use fetch_redirect_response=False to check</span>
<span class="sd">        such links without fetching them).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">host</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s2">&quot;The host argument is deprecated and no longer used by assertRedirects&quot;</span><span class="p">,</span>
                <span class="n">RemovedInDjango20Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="n">msg_prefix</span><span class="p">:</span>
            <span class="n">msg_prefix</span> <span class="o">+=</span> <span class="s2">&quot;: &quot;</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="s1">&#39;redirect_chain&#39;</span><span class="p">):</span>
            <span class="c1"># The request was a followed redirect</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">redirect_chain</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span>
                <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Response didn&#39;t redirect as expected: Response code was </span><span class="si">%d</span><span class="s2"> (expected </span><span class="si">%d</span><span class="s2">)&quot;</span>
                <span class="o">%</span> <span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">status_code</span><span class="p">)</span>
            <span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
                <span class="n">response</span><span class="o">.</span><span class="n">redirect_chain</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">status_code</span><span class="p">,</span>
                <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Initial response didn&#39;t redirect as expected: Response code was </span><span class="si">%d</span><span class="s2"> (expected </span><span class="si">%d</span><span class="s2">)&quot;</span>
                <span class="o">%</span> <span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">redirect_chain</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">status_code</span><span class="p">)</span>
            <span class="p">)</span>

            <span class="n">url</span><span class="p">,</span> <span class="n">status_code</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">redirect_chain</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span> <span class="o">=</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
                <span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">target_status_code</span><span class="p">,</span>
                <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Response didn&#39;t redirect as expected: Final Response code was </span><span class="si">%d</span><span class="s2"> (expected </span><span class="si">%d</span><span class="s2">)&quot;</span>
                <span class="o">%</span> <span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">target_status_code</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Not a followed redirect</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
                <span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">status_code</span><span class="p">,</span>
                <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Response didn&#39;t redirect as expected: Response code was </span><span class="si">%d</span><span class="s2"> (expected </span><span class="si">%d</span><span class="s2">)&quot;</span>
                <span class="o">%</span> <span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">status_code</span><span class="p">)</span>
            <span class="p">)</span>

            <span class="n">url</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">url</span>
            <span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span> <span class="o">=</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

            <span class="c1"># Prepend the request path to handle relative path redirects.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">):</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">urljoin</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">request</span><span class="p">[</span><span class="s1">&#39;PATH_INFO&#39;</span><span class="p">],</span> <span class="n">url</span><span class="p">)</span>
                <span class="n">path</span> <span class="o">=</span> <span class="n">urljoin</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">request</span><span class="p">[</span><span class="s1">&#39;PATH_INFO&#39;</span><span class="p">],</span> <span class="n">path</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">fetch_redirect_response</span><span class="p">:</span>
                <span class="c1"># netloc might be empty, or in cases where Django tests the</span>
                <span class="c1"># HTTP scheme, the convention is for netloc to be &#39;testserver&#39;.</span>
                <span class="c1"># Trust both as &quot;internal&quot; URLs here.</span>
                <span class="n">domain</span><span class="p">,</span> <span class="n">port</span> <span class="o">=</span> <span class="n">split_domain_port</span><span class="p">(</span><span class="n">netloc</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">domain</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">validate_host</span><span class="p">(</span><span class="n">domain</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">ALLOWED_HOSTS</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;The test client is unable to fetch remote URLs (got </span><span class="si">%s</span><span class="s2">). &quot;</span>
                        <span class="s2">&quot;If the host is served by Django, add &#39;</span><span class="si">%s</span><span class="s2">&#39; to ALLOWED_HOSTS. &quot;</span>
                        <span class="s2">&quot;Otherwise, use assertRedirects(..., fetch_redirect_response=False).&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">domain</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="n">redirect_response</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">QueryDict</span><span class="p">(</span><span class="n">query</span><span class="p">),</span> <span class="n">secure</span><span class="o">=</span><span class="p">(</span><span class="n">scheme</span> <span class="o">==</span> <span class="s1">&#39;https&#39;</span><span class="p">))</span>

                <span class="c1"># Get the redirection page, using the same client that was used</span>
                <span class="c1"># to obtain the original response.</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
                    <span class="n">redirect_response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">target_status_code</span><span class="p">,</span>
                    <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Couldn&#39;t retrieve redirection page &#39;</span><span class="si">%s</span><span class="s2">&#39;: response code was </span><span class="si">%d</span><span class="s2"> (expected </span><span class="si">%d</span><span class="s2">)&quot;</span>
                    <span class="o">%</span> <span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">redirect_response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">target_status_code</span><span class="p">)</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="n">url</span> <span class="o">!=</span> <span class="n">expected_url</span><span class="p">:</span>
            <span class="c1"># For temporary backwards compatibility, try to compare with a relative url</span>
            <span class="n">e_scheme</span><span class="p">,</span> <span class="n">e_netloc</span><span class="p">,</span> <span class="n">e_path</span><span class="p">,</span> <span class="n">e_query</span><span class="p">,</span> <span class="n">e_fragment</span> <span class="o">=</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">expected_url</span><span class="p">)</span>
            <span class="n">relative_url</span> <span class="o">=</span> <span class="n">urlunsplit</span><span class="p">((</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">e_path</span><span class="p">,</span> <span class="n">e_query</span><span class="p">,</span> <span class="n">e_fragment</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">url</span> <span class="o">==</span> <span class="n">relative_url</span><span class="p">:</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;assertRedirects had to strip the scheme and domain from the &quot;</span>
                    <span class="s2">&quot;expected URL, as it was always added automatically to URLs &quot;</span>
                    <span class="s2">&quot;before Django 1.9. Please update your expected URLs by &quot;</span>
                    <span class="s2">&quot;removing the scheme and domain.&quot;</span><span class="p">,</span>
                    <span class="n">RemovedInDjango20Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
                <span class="n">expected_url</span> <span class="o">=</span> <span class="n">relative_url</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
            <span class="n">url</span><span class="p">,</span> <span class="n">expected_url</span><span class="p">,</span>
            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Response redirected to &#39;</span><span class="si">%s</span><span class="s2">&#39;, expected &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">expected_url</span><span class="p">)</span>
        <span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_assert_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">status_code</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="p">,</span> <span class="n">html</span><span class="p">):</span>
        <span class="c1"># If the response supports deferred rendering and hasn&#39;t been rendered</span>
        <span class="c1"># yet, then ensure that it does get rendered before proceeding further.</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="s1">&#39;render&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">callable</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">render</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">response</span><span class="o">.</span><span class="n">is_rendered</span><span class="p">:</span>
            <span class="n">response</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">msg_prefix</span><span class="p">:</span>
            <span class="n">msg_prefix</span> <span class="o">+=</span> <span class="s2">&quot;: &quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
            <span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">status_code</span><span class="p">,</span>
            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Couldn&#39;t retrieve content: Response code was </span><span class="si">%d</span><span class="s2">&quot;</span>
            <span class="s2">&quot; (expected </span><span class="si">%d</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="n">status_code</span><span class="p">)</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">streaming</span><span class="p">:</span>
            <span class="n">content</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">streaming_content</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">content</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">content</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span> <span class="ow">or</span> <span class="n">html</span><span class="p">:</span>
            <span class="n">text</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">response</span><span class="o">.</span><span class="n">charset</span><span class="p">)</span>
            <span class="n">content</span> <span class="o">=</span> <span class="n">content</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">charset</span><span class="p">)</span>
            <span class="n">text_repr</span> <span class="o">=</span> <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="n">text</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">text_repr</span> <span class="o">=</span> <span class="nb">repr</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">html</span><span class="p">:</span>
            <span class="n">content</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="s2">&quot;Response&#39;s content is not valid HTML:&quot;</span><span class="p">)</span>
            <span class="n">text</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="s2">&quot;Second argument is not valid HTML:&quot;</span><span class="p">)</span>
        <span class="n">real_count</span> <span class="o">=</span> <span class="n">content</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">text_repr</span><span class="p">,</span> <span class="n">real_count</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="p">)</span>

<div class="viewcode-block" id="SimpleTestCase.assertContains"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertContains">[docs]</a>    <span class="k">def</span> <span class="nf">assertContains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">status_code</span><span class="o">=</span><span class="mi">200</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">html</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that a response indicates that some content was retrieved</span>
<span class="sd">        successfully, (i.e., the HTTP status code was as expected), and that</span>
<span class="sd">        ``text`` occurs ``count`` times in the content of the response.</span>
<span class="sd">        If ``count`` is None, the count doesn&#39;t matter - the assertion is true</span>
<span class="sd">        if the text occurs at least once in the response.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">text_repr</span><span class="p">,</span> <span class="n">real_count</span><span class="p">,</span> <span class="n">msg_prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_assert_contains</span><span class="p">(</span>
            <span class="n">response</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">status_code</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">count</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
                <span class="n">real_count</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span>
                <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Found </span><span class="si">%d</span><span class="s2"> instances of </span><span class="si">%s</span><span class="s2"> in response (expected </span><span class="si">%d</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">real_count</span><span class="p">,</span> <span class="n">text_repr</span><span class="p">,</span> <span class="n">count</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="n">real_count</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Couldn&#39;t find </span><span class="si">%s</span><span class="s2"> in response&quot;</span> <span class="o">%</span> <span class="n">text_repr</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertNotContains"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertNotContains">[docs]</a>    <span class="k">def</span> <span class="nf">assertNotContains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">status_code</span><span class="o">=</span><span class="mi">200</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">html</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that a response indicates that some content was retrieved</span>
<span class="sd">        successfully, (i.e., the HTTP status code was as expected), and that</span>
<span class="sd">        ``text`` doesn&#39;t occurs in the content of the response.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">text_repr</span><span class="p">,</span> <span class="n">real_count</span><span class="p">,</span> <span class="n">msg_prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_assert_contains</span><span class="p">(</span>
            <span class="n">response</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">status_code</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">real_count</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Response should not contain </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">text_repr</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertFormError"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertFormError">[docs]</a>    <span class="k">def</span> <span class="nf">assertFormError</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">errors</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that a form used to render the response has a specific field</span>
<span class="sd">        error.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">msg_prefix</span><span class="p">:</span>
            <span class="n">msg_prefix</span> <span class="o">+=</span> <span class="s2">&quot;: &quot;</span>

        <span class="c1"># Put context(s) into a list to simplify processing.</span>
        <span class="n">contexts</span> <span class="o">=</span> <span class="n">to_list</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">context</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">contexts</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Response did not use any contexts to render the response&quot;</span><span class="p">)</span>

        <span class="c1"># Put error(s) into a list to simplify processing.</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="n">to_list</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>

        <span class="c1"># Search all contexts for the error.</span>
        <span class="n">found_form</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">context</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">contexts</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">form</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">context</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="n">found_form</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">for</span> <span class="n">err</span> <span class="ow">in</span> <span class="n">errors</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">field</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">context</span><span class="p">[</span><span class="n">form</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span><span class="p">:</span>
                        <span class="n">field_errors</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="n">form</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span>
                            <span class="n">err</span> <span class="ow">in</span> <span class="n">field_errors</span><span class="p">,</span>
                            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The field &#39;</span><span class="si">%s</span><span class="s2">&#39; on form &#39;</span><span class="si">%s</span><span class="s2">&#39; in&quot;</span>
                            <span class="s2">&quot; context </span><span class="si">%d</span><span class="s2"> does not contain the error &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span>
                            <span class="s2">&quot; (actual errors: </span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">field_errors</span><span class="p">))</span>
                        <span class="p">)</span>
                    <span class="k">elif</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">context</span><span class="p">[</span><span class="n">form</span><span class="p">]</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span>
                            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The field &#39;</span><span class="si">%s</span><span class="s2">&#39; on form &#39;</span><span class="si">%s</span><span class="s2">&#39; in context </span><span class="si">%d</span><span class="s2"> contains no errors&quot;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span>
                            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The form &#39;</span><span class="si">%s</span><span class="s2">&#39; in context </span><span class="si">%d</span><span class="s2"> does not contain the field &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">field</span><span class="p">)</span>
                        <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">non_field_errors</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="n">form</span><span class="p">]</span><span class="o">.</span><span class="n">non_field_errors</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span>
                        <span class="n">err</span> <span class="ow">in</span> <span class="n">non_field_errors</span><span class="p">,</span>
                        <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The form &#39;</span><span class="si">%s</span><span class="s2">&#39; in context </span><span class="si">%d</span><span class="s2"> does not&quot;</span>
                        <span class="s2">&quot; contain the non-field error &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span>
                        <span class="s2">&quot; (actual errors: </span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="n">non_field_errors</span><span class="p">)</span>
                    <span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">found_form</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The form &#39;</span><span class="si">%s</span><span class="s2">&#39; was not used to render the response&quot;</span> <span class="o">%</span> <span class="n">form</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertFormsetError"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertFormsetError">[docs]</a>    <span class="k">def</span> <span class="nf">assertFormsetError</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">form_index</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">errors</span><span class="p">,</span>
                           <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that a formset used to render the response has a specific error.</span>

<span class="sd">        For field errors, specify the ``form_index`` and the ``field``.</span>
<span class="sd">        For non-field errors, specify the ``form_index`` and the ``field`` as</span>
<span class="sd">        None.</span>
<span class="sd">        For non-form errors, specify ``form_index`` as None and the ``field``</span>
<span class="sd">        as None.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Add punctuation to msg_prefix</span>
        <span class="k">if</span> <span class="n">msg_prefix</span><span class="p">:</span>
            <span class="n">msg_prefix</span> <span class="o">+=</span> <span class="s2">&quot;: &quot;</span>

        <span class="c1"># Put context(s) into a list to simplify processing.</span>
        <span class="n">contexts</span> <span class="o">=</span> <span class="n">to_list</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">context</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">contexts</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="n">msg_prefix</span> <span class="o">+</span> <span class="s1">&#39;Response did not use any contexts to &#39;</span>
                      <span class="s1">&#39;render the response&#39;</span><span class="p">)</span>

        <span class="c1"># Put error(s) into a list to simplify processing.</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="n">to_list</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>

        <span class="c1"># Search all contexts for the error.</span>
        <span class="n">found_formset</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">context</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">contexts</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">formset</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">context</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="n">found_formset</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">for</span> <span class="n">err</span> <span class="ow">in</span> <span class="n">errors</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">field</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">context</span><span class="p">[</span><span class="n">formset</span><span class="p">]</span><span class="o">.</span><span class="n">forms</span><span class="p">[</span><span class="n">form_index</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span><span class="p">:</span>
                        <span class="n">field_errors</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="n">formset</span><span class="p">]</span><span class="o">.</span><span class="n">forms</span><span class="p">[</span><span class="n">form_index</span><span class="p">]</span><span class="o">.</span><span class="n">errors</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span>
                            <span class="n">err</span> <span class="ow">in</span> <span class="n">field_errors</span><span class="p">,</span>
                            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The field &#39;</span><span class="si">%s</span><span class="s2">&#39; on formset &#39;</span><span class="si">%s</span><span class="s2">&#39;, &quot;</span>
                            <span class="s2">&quot;form </span><span class="si">%d</span><span class="s2"> in context </span><span class="si">%d</span><span class="s2"> does not contain the &quot;</span>
                            <span class="s2">&quot;error &#39;</span><span class="si">%s</span><span class="s2">&#39; (actual errors: </span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">form_index</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">field_errors</span><span class="p">))</span>
                        <span class="p">)</span>
                    <span class="k">elif</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">context</span><span class="p">[</span><span class="n">formset</span><span class="p">]</span><span class="o">.</span><span class="n">forms</span><span class="p">[</span><span class="n">form_index</span><span class="p">]</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span>
                            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The field &#39;</span><span class="si">%s</span><span class="s2">&#39; on formset &#39;</span><span class="si">%s</span><span class="s2">&#39;, form </span><span class="si">%d</span><span class="s2"> in context </span><span class="si">%d</span><span class="s2"> contains no errors&quot;</span>
                            <span class="o">%</span> <span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">form_index</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span>
                            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The formset &#39;</span><span class="si">%s</span><span class="s2">&#39;, form </span><span class="si">%d</span><span class="s2"> in context </span><span class="si">%d</span><span class="s2"> does not contain the field &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span>
                            <span class="o">%</span> <span class="p">(</span><span class="n">formset</span><span class="p">,</span> <span class="n">form_index</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">field</span><span class="p">)</span>
                        <span class="p">)</span>
                <span class="k">elif</span> <span class="n">form_index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">non_field_errors</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="n">formset</span><span class="p">]</span><span class="o">.</span><span class="n">forms</span><span class="p">[</span><span class="n">form_index</span><span class="p">]</span><span class="o">.</span><span class="n">non_field_errors</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">assertFalse</span><span class="p">(</span>
                        <span class="nb">len</span><span class="p">(</span><span class="n">non_field_errors</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">,</span>
                        <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The formset &#39;</span><span class="si">%s</span><span class="s2">&#39;, form </span><span class="si">%d</span><span class="s2"> in context </span><span class="si">%d</span><span class="s2"> &quot;</span>
                        <span class="s2">&quot;does not contain any non-field errors.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">formset</span><span class="p">,</span> <span class="n">form_index</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
                    <span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span>
                        <span class="n">err</span> <span class="ow">in</span> <span class="n">non_field_errors</span><span class="p">,</span>
                        <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The formset &#39;</span><span class="si">%s</span><span class="s2">&#39;, form </span><span class="si">%d</span><span class="s2"> in context </span><span class="si">%d</span><span class="s2"> &quot;</span>
                        <span class="s2">&quot;does not contain the non-field error &#39;</span><span class="si">%s</span><span class="s2">&#39; (actual errors: </span><span class="si">%s</span><span class="s2">)&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="n">formset</span><span class="p">,</span> <span class="n">form_index</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">non_field_errors</span><span class="p">))</span>
                    <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">non_form_errors</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="n">formset</span><span class="p">]</span><span class="o">.</span><span class="n">non_form_errors</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">assertFalse</span><span class="p">(</span>
                        <span class="nb">len</span><span class="p">(</span><span class="n">non_form_errors</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">,</span>
                        <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The formset &#39;</span><span class="si">%s</span><span class="s2">&#39; in context </span><span class="si">%d</span><span class="s2"> does not &quot;</span>
                        <span class="s2">&quot;contain any non-form errors.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">formset</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
                    <span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span>
                        <span class="n">err</span> <span class="ow">in</span> <span class="n">non_form_errors</span><span class="p">,</span>
                        <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The formset &#39;</span><span class="si">%s</span><span class="s2">&#39; in context </span><span class="si">%d</span><span class="s2"> does not &quot;</span>
                        <span class="s2">&quot;contain the non-form error &#39;</span><span class="si">%s</span><span class="s2">&#39; (actual errors: </span><span class="si">%s</span><span class="s2">)&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="n">formset</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">non_form_errors</span><span class="p">))</span>
                    <span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">found_formset</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;The formset &#39;</span><span class="si">%s</span><span class="s2">&#39; was not used to render the response&quot;</span> <span class="o">%</span> <span class="n">formset</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_assert_template_used</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="p">):</span>

        <span class="k">if</span> <span class="n">response</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">template_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;response and/or template_name argument must be provided&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">msg_prefix</span><span class="p">:</span>
            <span class="n">msg_prefix</span> <span class="o">+=</span> <span class="s2">&quot;: &quot;</span>

        <span class="k">if</span> <span class="n">template_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">response</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="s1">&#39;templates&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;assertTemplateUsed() and assertTemplateNotUsed() are only &quot;</span>
                <span class="s2">&quot;usable on responses fetched using the Django test Client.&quot;</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="s1">&#39;templates&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">response</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">template_name</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">response</span><span class="p">:</span>
                <span class="n">template_name</span> <span class="o">=</span> <span class="n">response</span>
                <span class="n">response</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="c1"># use this template with context manager</span>
            <span class="k">return</span> <span class="n">template_name</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">msg_prefix</span>

        <span class="n">template_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">response</span><span class="o">.</span><span class="n">templates</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">]</span>
        <span class="k">return</span> <span class="kc">None</span><span class="p">,</span> <span class="n">template_names</span><span class="p">,</span> <span class="n">msg_prefix</span>

<div class="viewcode-block" id="SimpleTestCase.assertTemplateUsed"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertTemplateUsed">[docs]</a>    <span class="k">def</span> <span class="nf">assertTemplateUsed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that the template with the provided name was used in rendering</span>
<span class="sd">        the response. Also usable as context manager.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">context_mgr_template</span><span class="p">,</span> <span class="n">template_names</span><span class="p">,</span> <span class="n">msg_prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_assert_template_used</span><span class="p">(</span>
            <span class="n">response</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">context_mgr_template</span><span class="p">:</span>
            <span class="c1"># Use assertTemplateUsed as context manager.</span>
            <span class="k">return</span> <span class="n">_AssertTemplateUsedContext</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context_mgr_template</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">template_names</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;No templates used to render the response&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span>
            <span class="n">template_name</span> <span class="ow">in</span> <span class="n">template_names</span><span class="p">,</span>
            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Template &#39;</span><span class="si">%s</span><span class="s2">&#39; was not a template used to render&quot;</span>
            <span class="s2">&quot; the response. Actual template(s) used: </span><span class="si">%s</span><span class="s2">&quot;</span>
            <span class="o">%</span> <span class="p">(</span><span class="n">template_name</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">template_names</span><span class="p">))</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="n">count</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
                <span class="n">template_names</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">template_name</span><span class="p">),</span> <span class="n">count</span><span class="p">,</span>
                <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Template &#39;</span><span class="si">%s</span><span class="s2">&#39; was expected to be rendered </span><span class="si">%d</span><span class="s2"> &quot;</span>
                <span class="s2">&quot;time(s) but was actually rendered </span><span class="si">%d</span><span class="s2"> time(s).&quot;</span>
                <span class="o">%</span> <span class="p">(</span><span class="n">template_name</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">template_names</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">template_name</span><span class="p">))</span>
            <span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertTemplateNotUsed"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertTemplateNotUsed">[docs]</a>    <span class="k">def</span> <span class="nf">assertTemplateNotUsed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that the template with the provided name was NOT used in</span>
<span class="sd">        rendering the response. Also usable as context manager.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">context_mgr_template</span><span class="p">,</span> <span class="n">template_names</span><span class="p">,</span> <span class="n">msg_prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_assert_template_used</span><span class="p">(</span>
            <span class="n">response</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">msg_prefix</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">context_mgr_template</span><span class="p">:</span>
            <span class="c1"># Use assertTemplateNotUsed as context manager.</span>
            <span class="k">return</span> <span class="n">_AssertTemplateNotUsedContext</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context_mgr_template</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">assertFalse</span><span class="p">(</span>
            <span class="n">template_name</span> <span class="ow">in</span> <span class="n">template_names</span><span class="p">,</span>
            <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Template &#39;</span><span class="si">%s</span><span class="s2">&#39; was used unexpectedly in rendering the response&quot;</span> <span class="o">%</span> <span class="n">template_name</span>
        <span class="p">)</span></div>

    <span class="nd">@contextmanager</span>
    <span class="k">def</span> <span class="nf">_assert_raises_message_cm</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expected_exception</span><span class="p">,</span> <span class="n">expected_message</span><span class="p">):</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="n">expected_exception</span><span class="p">)</span> <span class="k">as</span> <span class="n">cm</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">cm</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertIn</span><span class="p">(</span><span class="n">expected_message</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">cm</span><span class="o">.</span><span class="n">exception</span><span class="p">))</span>

<div class="viewcode-block" id="SimpleTestCase.assertRaisesMessage"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertRaisesMessage">[docs]</a>    <span class="k">def</span> <span class="nf">assertRaisesMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expected_exception</span><span class="p">,</span> <span class="n">expected_message</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that expected_message is found in the the message of a raised</span>
<span class="sd">        exception.</span>

<span class="sd">        Args:</span>
<span class="sd">            expected_exception: Exception class expected to be raised.</span>
<span class="sd">            expected_message: expected error message string value.</span>
<span class="sd">            args: Function to be called and extra positional args.</span>
<span class="sd">            kwargs: Extra kwargs.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># callable_obj was a documented kwarg in Django 1.8 and older.</span>
        <span class="n">callable_obj</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;callable_obj&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">callable_obj</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s1">&#39;The callable_obj kwarg is deprecated. Pass the callable &#39;</span>
                <span class="s1">&#39;as a positional argument instead.&#39;</span><span class="p">,</span> <span class="n">RemovedInDjango20Warning</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
            <span class="n">callable_obj</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">args</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>

        <span class="n">cm</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_assert_raises_message_cm</span><span class="p">(</span><span class="n">expected_exception</span><span class="p">,</span> <span class="n">expected_message</span><span class="p">)</span>
        <span class="c1"># Assertion used in context manager fashion.</span>
        <span class="k">if</span> <span class="n">callable_obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">cm</span>
        <span class="c1"># Assertion was passed a callable.</span>
        <span class="k">with</span> <span class="n">cm</span><span class="p">:</span>
            <span class="n">callable_obj</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertFieldOutput"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertFieldOutput">[docs]</a>    <span class="k">def</span> <span class="nf">assertFieldOutput</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fieldclass</span><span class="p">,</span> <span class="n">valid</span><span class="p">,</span> <span class="n">invalid</span><span class="p">,</span> <span class="n">field_args</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">field_kwargs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">empty_value</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that a form field behaves correctly with various inputs.</span>

<span class="sd">        Args:</span>
<span class="sd">            fieldclass: the class of the field to be tested.</span>
<span class="sd">            valid: a dictionary mapping valid inputs to their expected</span>
<span class="sd">                    cleaned values.</span>
<span class="sd">            invalid: a dictionary mapping invalid inputs to one or more</span>
<span class="sd">                    raised error messages.</span>
<span class="sd">            field_args: the args passed to instantiate the field</span>
<span class="sd">            field_kwargs: the kwargs passed to instantiate the field</span>
<span class="sd">            empty_value: the expected clean output for inputs in empty_values</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">field_args</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">field_args</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">field_kwargs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">field_kwargs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">required</span> <span class="o">=</span> <span class="n">fieldclass</span><span class="p">(</span><span class="o">*</span><span class="n">field_args</span><span class="p">,</span> <span class="o">**</span><span class="n">field_kwargs</span><span class="p">)</span>
        <span class="n">optional</span> <span class="o">=</span> <span class="n">fieldclass</span><span class="p">(</span><span class="o">*</span><span class="n">field_args</span><span class="p">,</span> <span class="o">**</span><span class="nb">dict</span><span class="p">(</span><span class="n">field_kwargs</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
        <span class="c1"># test valid inputs</span>
        <span class="k">for</span> <span class="nb">input</span><span class="p">,</span> <span class="n">output</span> <span class="ow">in</span> <span class="n">valid</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">required</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="nb">input</span><span class="p">),</span> <span class="n">output</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">optional</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="nb">input</span><span class="p">),</span> <span class="n">output</span><span class="p">)</span>
        <span class="c1"># test invalid inputs</span>
        <span class="k">for</span> <span class="nb">input</span><span class="p">,</span> <span class="n">errors</span> <span class="ow">in</span> <span class="n">invalid</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="n">ValidationError</span><span class="p">)</span> <span class="k">as</span> <span class="n">context_manager</span><span class="p">:</span>
                <span class="n">required</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">context_manager</span><span class="o">.</span><span class="n">exception</span><span class="o">.</span><span class="n">messages</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>

            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="n">ValidationError</span><span class="p">)</span> <span class="k">as</span> <span class="n">context_manager</span><span class="p">:</span>
                <span class="n">optional</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">context_manager</span><span class="o">.</span><span class="n">exception</span><span class="o">.</span><span class="n">messages</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>
        <span class="c1"># test required inputs</span>
        <span class="n">error_required</span> <span class="o">=</span> <span class="p">[</span><span class="n">force_text</span><span class="p">(</span><span class="n">required</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;required&#39;</span><span class="p">])]</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">required</span><span class="o">.</span><span class="n">empty_values</span><span class="p">:</span>
            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="n">ValidationError</span><span class="p">)</span> <span class="k">as</span> <span class="n">context_manager</span><span class="p">:</span>
                <span class="n">required</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">context_manager</span><span class="o">.</span><span class="n">exception</span><span class="o">.</span><span class="n">messages</span><span class="p">,</span> <span class="n">error_required</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">optional</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> <span class="n">empty_value</span><span class="p">)</span>
        <span class="c1"># test that max_length and min_length are always accepted</span>
        <span class="k">if</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">fieldclass</span><span class="p">,</span> <span class="n">CharField</span><span class="p">):</span>
            <span class="n">field_kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;min_length&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;max_length&#39;</span><span class="p">:</span> <span class="mi">20</span><span class="p">})</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertIsInstance</span><span class="p">(</span><span class="n">fieldclass</span><span class="p">(</span><span class="o">*</span><span class="n">field_args</span><span class="p">,</span> <span class="o">**</span><span class="n">field_kwargs</span><span class="p">),</span> <span class="n">fieldclass</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertHTMLEqual"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertHTMLEqual">[docs]</a>    <span class="k">def</span> <span class="nf">assertHTMLEqual</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html1</span><span class="p">,</span> <span class="n">html2</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that two HTML snippets are semantically the same.</span>
<span class="sd">        Whitespace in most cases is ignored, and attribute ordering is not</span>
<span class="sd">        significant. The passed-in arguments must be valid HTML.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">dom1</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html1</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="s1">&#39;First argument is not valid HTML:&#39;</span><span class="p">)</span>
        <span class="n">dom2</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html2</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="s1">&#39;Second argument is not valid HTML:&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">dom1</span> <span class="o">!=</span> <span class="n">dom2</span><span class="p">:</span>
            <span class="n">standardMsg</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> != </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">safe_repr</span><span class="p">(</span><span class="n">dom1</span><span class="p">,</span> <span class="kc">True</span><span class="p">),</span> <span class="n">safe_repr</span><span class="p">(</span><span class="n">dom2</span><span class="p">,</span> <span class="kc">True</span><span class="p">))</span>
            <span class="n">diff</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">difflib</span><span class="o">.</span><span class="n">ndiff</span><span class="p">(</span>
                <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">dom1</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(),</span>
                <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">dom2</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(),</span>
            <span class="p">)))</span>
            <span class="n">standardMsg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_truncateMessage</span><span class="p">(</span><span class="n">standardMsg</span><span class="p">,</span> <span class="n">diff</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_formatMessage</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">standardMsg</span><span class="p">))</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertHTMLNotEqual"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertHTMLNotEqual">[docs]</a>    <span class="k">def</span> <span class="nf">assertHTMLNotEqual</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html1</span><span class="p">,</span> <span class="n">html2</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Asserts that two HTML snippets are not semantically equivalent.&quot;&quot;&quot;</span>
        <span class="n">dom1</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html1</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="s1">&#39;First argument is not valid HTML:&#39;</span><span class="p">)</span>
        <span class="n">dom2</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">html2</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="s1">&#39;Second argument is not valid HTML:&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">dom1</span> <span class="o">==</span> <span class="n">dom2</span><span class="p">:</span>
            <span class="n">standardMsg</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> == </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">safe_repr</span><span class="p">(</span><span class="n">dom1</span><span class="p">,</span> <span class="kc">True</span><span class="p">),</span> <span class="n">safe_repr</span><span class="p">(</span><span class="n">dom2</span><span class="p">,</span> <span class="kc">True</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_formatMessage</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">standardMsg</span><span class="p">))</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertInHTML"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertInHTML">[docs]</a>    <span class="k">def</span> <span class="nf">assertInHTML</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">needle</span><span class="p">,</span> <span class="n">haystack</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">msg_prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="n">needle</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">needle</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;First argument is not valid HTML:&#39;</span><span class="p">)</span>
        <span class="n">haystack</span> <span class="o">=</span> <span class="n">assert_and_parse_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">haystack</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;Second argument is not valid HTML:&#39;</span><span class="p">)</span>
        <span class="n">real_count</span> <span class="o">=</span> <span class="n">haystack</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">needle</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">count</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
                <span class="n">real_count</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span>
                <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Found </span><span class="si">%d</span><span class="s2"> instances of &#39;</span><span class="si">%s</span><span class="s2">&#39; in response (expected </span><span class="si">%d</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">real_count</span><span class="p">,</span> <span class="n">needle</span><span class="p">,</span> <span class="n">count</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="n">real_count</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">msg_prefix</span> <span class="o">+</span> <span class="s2">&quot;Couldn&#39;t find &#39;</span><span class="si">%s</span><span class="s2">&#39; in response&quot;</span> <span class="o">%</span> <span class="n">needle</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertJSONEqual"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertJSONEqual">[docs]</a>    <span class="k">def</span> <span class="nf">assertJSONEqual</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">raw</span><span class="p">,</span> <span class="n">expected_data</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that the JSON fragments raw and expected_data are equal.</span>
<span class="sd">        Usual JSON non-significant whitespace rules apply as the heavyweight</span>
<span class="sd">        is delegated to the json library.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">raw</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="s2">&quot;First argument is not valid JSON: </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">raw</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expected_data</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">expected_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">expected_data</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="s2">&quot;Second argument is not valid JSON: </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">expected_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">expected_data</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="n">msg</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertJSONNotEqual"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertJSONNotEqual">[docs]</a>    <span class="k">def</span> <span class="nf">assertJSONNotEqual</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">raw</span><span class="p">,</span> <span class="n">expected_data</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that the JSON fragments raw and expected_data are not equal.</span>
<span class="sd">        Usual JSON non-significant whitespace rules apply as the heavyweight</span>
<span class="sd">        is delegated to the json library.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">raw</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="s2">&quot;First argument is not valid JSON: </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">raw</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">expected_data</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">expected_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">expected_data</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="s2">&quot;Second argument is not valid JSON: </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">expected_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertNotEqual</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">expected_data</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="n">msg</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertXMLEqual"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertXMLEqual">[docs]</a>    <span class="k">def</span> <span class="nf">assertXMLEqual</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xml1</span><span class="p">,</span> <span class="n">xml2</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that two XML snippets are semantically the same.</span>
<span class="sd">        Whitespace in most cases is ignored, and attribute ordering is not</span>
<span class="sd">        significant. The passed-in arguments must be valid XML.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">compare_xml</span><span class="p">(</span><span class="n">xml1</span><span class="p">,</span> <span class="n">xml2</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">standardMsg</span> <span class="o">=</span> <span class="s1">&#39;First or second argument is not valid XML</span><span class="se">\n</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">e</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_formatMessage</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">standardMsg</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">result</span><span class="p">:</span>
                <span class="n">standardMsg</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> != </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">safe_repr</span><span class="p">(</span><span class="n">xml1</span><span class="p">,</span> <span class="kc">True</span><span class="p">),</span> <span class="n">safe_repr</span><span class="p">(</span><span class="n">xml2</span><span class="p">,</span> <span class="kc">True</span><span class="p">))</span>
                <span class="n">diff</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
                    <span class="n">difflib</span><span class="o">.</span><span class="n">ndiff</span><span class="p">(</span>
                        <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">xml1</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(),</span>
                        <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">xml2</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(),</span>
                    <span class="p">)</span>
                <span class="p">))</span>
                <span class="n">standardMsg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_truncateMessage</span><span class="p">(</span><span class="n">standardMsg</span><span class="p">,</span> <span class="n">diff</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_formatMessage</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">standardMsg</span><span class="p">))</span></div>

<div class="viewcode-block" id="SimpleTestCase.assertXMLNotEqual"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.SimpleTestCase.assertXMLNotEqual">[docs]</a>    <span class="k">def</span> <span class="nf">assertXMLNotEqual</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xml1</span><span class="p">,</span> <span class="n">xml2</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Asserts that two XML snippets are not semantically equivalent.</span>
<span class="sd">        Whitespace in most cases is ignored, and attribute ordering is not</span>
<span class="sd">        significant. The passed-in arguments must be valid XML.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">compare_xml</span><span class="p">(</span><span class="n">xml1</span><span class="p">,</span> <span class="n">xml2</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">standardMsg</span> <span class="o">=</span> <span class="s1">&#39;First or second argument is not valid XML</span><span class="se">\n</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">e</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_formatMessage</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">standardMsg</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">result</span><span class="p">:</span>
                <span class="n">standardMsg</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> == </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">safe_repr</span><span class="p">(</span><span class="n">xml1</span><span class="p">,</span> <span class="kc">True</span><span class="p">),</span> <span class="n">safe_repr</span><span class="p">(</span><span class="n">xml2</span><span class="p">,</span> <span class="kc">True</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_formatMessage</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">standardMsg</span><span class="p">))</span></div>

    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
        <span class="n">assertCountEqual</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="o">.</span><span class="n">assertItemsEqual</span>
        <span class="n">assertNotRegex</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="o">.</span><span class="n">assertNotRegexpMatches</span>
        <span class="n">assertRaisesRegex</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="o">.</span><span class="n">assertRaisesRegexp</span>
        <span class="n">assertRegex</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="o">.</span><span class="n">assertRegexpMatches</span></div>


<div class="viewcode-block" id="TransactionTestCase"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.TransactionTestCase">[docs]</a><span class="k">class</span> <span class="nc">TransactionTestCase</span><span class="p">(</span><span class="n">SimpleTestCase</span><span class="p">):</span>

    <span class="c1"># Subclasses can ask for resetting of auto increment sequence before each</span>
    <span class="c1"># test case</span>
    <span class="n">reset_sequences</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="c1"># Subclasses can enable only a subset of apps for faster tests</span>
    <span class="n">available_apps</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="c1"># Subclasses can define fixtures which will be automatically installed.</span>
    <span class="n">fixtures</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="c1"># If transactions aren&#39;t available, Django will serialize the database</span>
    <span class="c1"># contents into a fixture during setup and flush and reload them</span>
    <span class="c1"># during teardown (as flush does not restore data from migrations).</span>
    <span class="c1"># This can be slow; this flag allows enabling on a per-case basis.</span>
    <span class="n">serialized_rollback</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="c1"># Since tests will be wrapped in a transaction, or serialized if they</span>
    <span class="c1"># are not available, we allow queries to be run.</span>
    <span class="n">allow_database_queries</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_pre_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Performs any pre-test setup. This includes:</span>

<span class="sd">        * If the class has an &#39;available_apps&#39; attribute, restricting the app</span>
<span class="sd">          registry to these applications, then firing post_migrate -- it must</span>
<span class="sd">          run with the correct set of applications for the test case.</span>
<span class="sd">        * If the class has a &#39;fixtures&#39; attribute, installing these fixtures.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">TransactionTestCase</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_pre_setup</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">apps</span><span class="o">.</span><span class="n">set_available_apps</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span><span class="p">)</span>
            <span class="n">setting_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
                <span class="n">sender</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span>
                <span class="n">setting</span><span class="o">=</span><span class="s1">&#39;INSTALLED_APPS&#39;</span><span class="p">,</span>
                <span class="n">value</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span><span class="p">,</span>
                <span class="n">enter</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">for</span> <span class="n">db_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_databases_names</span><span class="p">(</span><span class="n">include_mirrors</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
                <span class="n">emit_post_migrate_signal</span><span class="p">(</span><span class="n">verbosity</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">interactive</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="n">db_name</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fixture_setup</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">apps</span><span class="o">.</span><span class="n">unset_available_apps</span><span class="p">()</span>
                <span class="n">setting_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
                    <span class="n">sender</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span>
                    <span class="n">setting</span><span class="o">=</span><span class="s1">&#39;INSTALLED_APPS&#39;</span><span class="p">,</span>
                    <span class="n">value</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">INSTALLED_APPS</span><span class="p">,</span>
                    <span class="n">enter</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="k">raise</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_databases_names</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">include_mirrors</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="c1"># If the test case has a multi_db=True flag, act on all databases,</span>
        <span class="c1"># including mirrors or not. Otherwise, just on the default DB.</span>
        <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;multi_db&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">alias</span> <span class="k">for</span> <span class="n">alias</span> <span class="ow">in</span> <span class="n">connections</span>
                <span class="k">if</span> <span class="n">include_mirrors</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">connections</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span><span class="o">.</span><span class="n">settings_dict</span><span class="p">[</span><span class="s1">&#39;TEST&#39;</span><span class="p">][</span><span class="s1">&#39;MIRROR&#39;</span><span class="p">]</span>
            <span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">DEFAULT_DB_ALIAS</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_reset_sequences</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_name</span><span class="p">):</span>
        <span class="n">conn</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">db_name</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">conn</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_sequence_reset</span><span class="p">:</span>
            <span class="n">sql_list</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">sequence_reset_by_name_sql</span><span class="p">(</span>
                <span class="n">no_style</span><span class="p">(),</span> <span class="n">conn</span><span class="o">.</span><span class="n">introspection</span><span class="o">.</span><span class="n">sequence_list</span><span class="p">())</span>
            <span class="k">if</span> <span class="n">sql_list</span><span class="p">:</span>
                <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">db_name</span><span class="p">):</span>
                    <span class="n">cursor</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
                    <span class="k">for</span> <span class="n">sql</span> <span class="ow">in</span> <span class="n">sql_list</span><span class="p">:</span>
                        <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_fixture_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">db_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_databases_names</span><span class="p">(</span><span class="n">include_mirrors</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="c1"># Reset sequences</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reset_sequences</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_reset_sequences</span><span class="p">(</span><span class="n">db_name</span><span class="p">)</span>

            <span class="c1"># If we need to provide replica initial data from migrated apps,</span>
            <span class="c1"># then do so.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">serialized_rollback</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">connections</span><span class="p">[</span><span class="n">db_name</span><span class="p">],</span> <span class="s2">&quot;_test_serialized_contents&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">apps</span><span class="o">.</span><span class="n">unset_available_apps</span><span class="p">()</span>
                <span class="n">connections</span><span class="p">[</span><span class="n">db_name</span><span class="p">]</span><span class="o">.</span><span class="n">creation</span><span class="o">.</span><span class="n">deserialize_db_from_string</span><span class="p">(</span>
                    <span class="n">connections</span><span class="p">[</span><span class="n">db_name</span><span class="p">]</span><span class="o">.</span><span class="n">_test_serialized_contents</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">apps</span><span class="o">.</span><span class="n">set_available_apps</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fixtures</span><span class="p">:</span>
                <span class="c1"># We have to use this slightly awkward syntax due to the fact</span>
                <span class="c1"># that we&#39;re using *args and **kwargs together.</span>
                <span class="n">call_command</span><span class="p">(</span><span class="s1">&#39;loaddata&#39;</span><span class="p">,</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">fixtures</span><span class="p">,</span>
                             <span class="o">**</span><span class="p">{</span><span class="s1">&#39;verbosity&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;database&#39;</span><span class="p">:</span> <span class="n">db_name</span><span class="p">})</span>

    <span class="k">def</span> <span class="nf">_should_reload_connections</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_post_teardown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Performs any post-test things. This includes:</span>

<span class="sd">        * Flushing the contents of the database, to leave a clean slate. If</span>
<span class="sd">          the class has an &#39;available_apps&#39; attribute, post_migrate isn&#39;t fired.</span>
<span class="sd">        * Force-closing the connection, so the next test gets a clean cursor.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fixture_teardown</span><span class="p">()</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">TransactionTestCase</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_post_teardown</span><span class="p">()</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_reload_connections</span><span class="p">():</span>
                <span class="c1"># Some DB cursors include SQL statements as part of cursor</span>
                <span class="c1"># creation. If you have a test that does a rollback, the effect</span>
                <span class="c1"># of these statements is lost, which can affect the operation of</span>
                <span class="c1"># tests (e.g., losing a timezone setting causing objects to be</span>
                <span class="c1"># created with the wrong time). To make sure this doesn&#39;t</span>
                <span class="c1"># happen, get a clean connection at the start of every test.</span>
                <span class="k">for</span> <span class="n">conn</span> <span class="ow">in</span> <span class="n">connections</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
                    <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">apps</span><span class="o">.</span><span class="n">unset_available_apps</span><span class="p">()</span>
                <span class="n">setting_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span>
                                     <span class="n">setting</span><span class="o">=</span><span class="s1">&#39;INSTALLED_APPS&#39;</span><span class="p">,</span>
                                     <span class="n">value</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">INSTALLED_APPS</span><span class="p">,</span>
                                     <span class="n">enter</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_fixture_teardown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Allow TRUNCATE ... CASCADE and don&#39;t emit the post_migrate signal</span>
        <span class="c1"># when flushing only a subset of the apps</span>
        <span class="k">for</span> <span class="n">db_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_databases_names</span><span class="p">(</span><span class="n">include_mirrors</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="c1"># Flush the database</span>
            <span class="n">inhibit_post_migrate</span> <span class="o">=</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span>
                <span class="p">(</span>   <span class="c1"># Inhibit the post_migrate signal when using serialized</span>
                    <span class="c1"># rollback to avoid trying to recreate the serialized data.</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">serialized_rollback</span> <span class="ow">and</span>
                    <span class="nb">hasattr</span><span class="p">(</span><span class="n">connections</span><span class="p">[</span><span class="n">db_name</span><span class="p">],</span> <span class="s1">&#39;_test_serialized_contents&#39;</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="n">call_command</span><span class="p">(</span><span class="s1">&#39;flush&#39;</span><span class="p">,</span> <span class="n">verbosity</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">interactive</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                         <span class="n">database</span><span class="o">=</span><span class="n">db_name</span><span class="p">,</span> <span class="n">reset_sequences</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                         <span class="n">allow_cascade</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">available_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span>
                         <span class="n">inhibit_post_migrate</span><span class="o">=</span><span class="n">inhibit_post_migrate</span><span class="p">)</span>

<div class="viewcode-block" id="TransactionTestCase.assertQuerysetEqual"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.TransactionTestCase.assertQuerysetEqual">[docs]</a>    <span class="k">def</span> <span class="nf">assertQuerysetEqual</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">qs</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">transform</span><span class="o">=</span><span class="nb">repr</span><span class="p">,</span> <span class="n">ordered</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">items</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">moves</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">transform</span><span class="p">,</span> <span class="n">qs</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ordered</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">Counter</span><span class="p">(</span><span class="n">items</span><span class="p">),</span> <span class="n">Counter</span><span class="p">(</span><span class="n">values</span><span class="p">),</span> <span class="n">msg</span><span class="o">=</span><span class="n">msg</span><span class="p">)</span>
        <span class="n">values</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
        <span class="c1"># For example qs.iterator() could be passed as qs, but it does not</span>
        <span class="c1"># have &#39;ordered&#39; attribute.</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">qs</span><span class="p">,</span> <span class="s1">&#39;ordered&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">qs</span><span class="o">.</span><span class="n">ordered</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Trying to compare non-ordered queryset &quot;</span>
                             <span class="s2">&quot;against more than one ordered values&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">items</span><span class="p">),</span> <span class="n">values</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="n">msg</span><span class="p">)</span></div>

<div class="viewcode-block" id="TransactionTestCase.assertNumQueries"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.TransactionTestCase.assertNumQueries">[docs]</a>    <span class="k">def</span> <span class="nf">assertNumQueries</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">using</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;using&quot;</span><span class="p">,</span> <span class="n">DEFAULT_DB_ALIAS</span><span class="p">)</span>
        <span class="n">conn</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">using</span><span class="p">]</span>

        <span class="n">context</span> <span class="o">=</span> <span class="n">_AssertNumQueriesContext</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">func</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">context</span>

        <span class="k">with</span> <span class="n">context</span><span class="p">:</span>
            <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div></div>


<span class="k">def</span> <span class="nf">connections_support_transactions</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns True if all connections support transactions.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">conn</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_transactions</span>
               <span class="k">for</span> <span class="n">conn</span> <span class="ow">in</span> <span class="n">connections</span><span class="o">.</span><span class="n">all</span><span class="p">())</span>


<div class="viewcode-block" id="TestCase"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.TestCase">[docs]</a><span class="k">class</span> <span class="nc">TestCase</span><span class="p">(</span><span class="n">TransactionTestCase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Similar to TransactionTestCase, but uses `transaction.atomic()` to achieve</span>
<span class="sd">    test isolation.</span>

<span class="sd">    In most situations, TestCase should be preferred to TransactionTestCase as</span>
<span class="sd">    it allows faster execution. However, there are some situations where using</span>
<span class="sd">    TransactionTestCase might be necessary (e.g. testing some transactional</span>
<span class="sd">    behavior).</span>

<span class="sd">    On database backends with no transaction support, TestCase behaves as</span>
<span class="sd">    TransactionTestCase.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_enter_atomics</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Helper method to open atomic blocks for multiple databases&quot;&quot;&quot;</span>
        <span class="n">atomics</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">db_name</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_databases_names</span><span class="p">():</span>
            <span class="n">atomics</span><span class="p">[</span><span class="n">db_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">db_name</span><span class="p">)</span>
            <span class="n">atomics</span><span class="p">[</span><span class="n">db_name</span><span class="p">]</span><span class="o">.</span><span class="fm">__enter__</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">atomics</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_rollback_atomics</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">atomics</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Rollback atomic blocks opened through the previous method&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">db_name</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_databases_names</span><span class="p">()):</span>
            <span class="n">transaction</span><span class="o">.</span><span class="n">set_rollback</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db_name</span><span class="p">)</span>
            <span class="n">atomics</span><span class="p">[</span><span class="n">db_name</span><span class="p">]</span><span class="o">.</span><span class="fm">__exit__</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">setUpClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">TestCase</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">setUpClass</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">connections_support_transactions</span><span class="p">():</span>
            <span class="k">return</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">cls_atomics</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_enter_atomics</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">fixtures</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">db_name</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_databases_names</span><span class="p">(</span><span class="n">include_mirrors</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">call_command</span><span class="p">(</span><span class="s1">&#39;loaddata&#39;</span><span class="p">,</span> <span class="o">*</span><span class="bp">cls</span><span class="o">.</span><span class="n">fixtures</span><span class="p">,</span> <span class="o">**</span><span class="p">{</span>
                        <span class="s1">&#39;verbosity&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
                        <span class="s1">&#39;commit&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                        <span class="s1">&#39;database&#39;</span><span class="p">:</span> <span class="n">db_name</span><span class="p">,</span>
                    <span class="p">})</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                    <span class="bp">cls</span><span class="o">.</span><span class="n">_rollback_atomics</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">cls_atomics</span><span class="p">)</span>
                    <span class="k">raise</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">setUpTestData</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_rollback_atomics</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">cls_atomics</span><span class="p">)</span>
            <span class="k">raise</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">tearDownClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">connections_support_transactions</span><span class="p">():</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_rollback_atomics</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">cls_atomics</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">conn</span> <span class="ow">in</span> <span class="n">connections</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
                <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">TestCase</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">tearDownClass</span><span class="p">()</span>

<div class="viewcode-block" id="TestCase.setUpTestData"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.TestCase.setUpTestData">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">setUpTestData</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Load initial data for the TestCase&quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

    <span class="k">def</span> <span class="nf">_should_reload_connections</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">connections_support_transactions</span><span class="p">():</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">TestCase</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_should_reload_connections</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_fixture_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">connections_support_transactions</span><span class="p">():</span>
            <span class="c1"># If the backend does not support transactions, we should reload</span>
            <span class="c1"># class data before each test</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setUpTestData</span><span class="p">()</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">TestCase</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_fixture_setup</span><span class="p">()</span>

        <span class="k">assert</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">reset_sequences</span><span class="p">,</span> <span class="s1">&#39;reset_sequences cannot be used on TestCase instances&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">atomics</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_enter_atomics</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_fixture_teardown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">connections_support_transactions</span><span class="p">():</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">TestCase</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_fixture_teardown</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">db_name</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_databases_names</span><span class="p">()):</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_check_constraints</span><span class="p">(</span><span class="n">connections</span><span class="p">[</span><span class="n">db_name</span><span class="p">]):</span>
                    <span class="n">connections</span><span class="p">[</span><span class="n">db_name</span><span class="p">]</span><span class="o">.</span><span class="n">check_constraints</span><span class="p">()</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_rollback_atomics</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">atomics</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_should_check_constraints</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">can_defer_constraint_checks</span> <span class="ow">and</span>
            <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span> <span class="ow">and</span> <span class="n">connection</span><span class="o">.</span><span class="n">is_usable</span><span class="p">()</span>
        <span class="p">)</span></div>


<span class="k">class</span> <span class="nc">CheckCondition</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Descriptor class for deferred condition checking&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">conditions</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conditions</span> <span class="o">=</span> <span class="n">conditions</span>

    <span class="k">def</span> <span class="nf">add_condition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">condition</span><span class="p">,</span> <span class="n">reason</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">conditions</span> <span class="o">+</span> <span class="p">((</span><span class="n">condition</span><span class="p">,</span> <span class="n">reason</span><span class="p">),))</span>

    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Trigger access for all bases.</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="s1">&#39;__unittest_skip__&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__bases__</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">for</span> <span class="n">condition</span><span class="p">,</span> <span class="n">reason</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">conditions</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">condition</span><span class="p">():</span>
                <span class="c1"># Override this descriptor&#39;s value and set the skip reason.</span>
                <span class="bp">cls</span><span class="o">.</span><span class="n">__unittest_skip__</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="bp">cls</span><span class="o">.</span><span class="n">__unittest_skip_why__</span> <span class="o">=</span> <span class="n">reason</span>
                <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">_deferredSkip</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">reason</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">test_func</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">test_func</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span> <span class="ow">and</span>
                <span class="nb">issubclass</span><span class="p">(</span><span class="n">test_func</span><span class="p">,</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">)):</span>
            <span class="nd">@wraps</span><span class="p">(</span><span class="n">test_func</span><span class="p">)</span>
            <span class="k">def</span> <span class="nf">skip_wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">condition</span><span class="p">():</span>
                    <span class="k">raise</span> <span class="n">unittest</span><span class="o">.</span><span class="n">SkipTest</span><span class="p">(</span><span class="n">reason</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">test_func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="n">test_item</span> <span class="o">=</span> <span class="n">skip_wrapper</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Assume a class is decorated</span>
            <span class="n">test_item</span> <span class="o">=</span> <span class="n">test_func</span>
            <span class="c1"># Retrieve the possibly existing value from the class&#39;s dict to</span>
            <span class="c1"># avoid triggering the descriptor.</span>
            <span class="n">skip</span> <span class="o">=</span> <span class="n">test_func</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;__unittest_skip__&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">skip</span><span class="p">,</span> <span class="n">CheckCondition</span><span class="p">):</span>
                <span class="n">test_item</span><span class="o">.</span><span class="n">__unittest_skip__</span> <span class="o">=</span> <span class="n">skip</span><span class="o">.</span><span class="n">add_condition</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="n">reason</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">skip</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">True</span><span class="p">:</span>
                <span class="n">test_item</span><span class="o">.</span><span class="n">__unittest_skip__</span> <span class="o">=</span> <span class="n">CheckCondition</span><span class="p">((</span><span class="n">condition</span><span class="p">,</span> <span class="n">reason</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">test_item</span>
    <span class="k">return</span> <span class="n">decorator</span>


<div class="viewcode-block" id="skipIfDBFeature"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.skipIfDBFeature">[docs]</a><span class="k">def</span> <span class="nf">skipIfDBFeature</span><span class="p">(</span><span class="o">*</span><span class="n">features</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Skip a test if a database has at least one of the named features.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_deferredSkip</span><span class="p">(</span>
        <span class="k">lambda</span><span class="p">:</span> <span class="nb">any</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="p">,</span> <span class="n">feature</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">features</span><span class="p">),</span>
        <span class="s2">&quot;Database has feature(s) </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">features</span><span class="p">)</span>
    <span class="p">)</span></div>


<div class="viewcode-block" id="skipUnlessDBFeature"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.skipUnlessDBFeature">[docs]</a><span class="k">def</span> <span class="nf">skipUnlessDBFeature</span><span class="p">(</span><span class="o">*</span><span class="n">features</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Skip a test unless a database has all the named features.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_deferredSkip</span><span class="p">(</span>
        <span class="k">lambda</span><span class="p">:</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="p">,</span> <span class="n">feature</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">features</span><span class="p">),</span>
        <span class="s2">&quot;Database doesn&#39;t support feature(s): </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">features</span><span class="p">)</span>
    <span class="p">)</span></div>


<span class="k">def</span> <span class="nf">skipUnlessAnyDBFeature</span><span class="p">(</span><span class="o">*</span><span class="n">features</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Skip a test unless a database has any of the named features.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_deferredSkip</span><span class="p">(</span>
        <span class="k">lambda</span><span class="p">:</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="p">,</span> <span class="n">feature</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">features</span><span class="p">),</span>
        <span class="s2">&quot;Database doesn&#39;t support any of the feature(s): </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">features</span><span class="p">)</span>
    <span class="p">)</span>


<span class="k">class</span> <span class="nc">QuietWSGIRequestHandler</span><span class="p">(</span><span class="n">WSGIRequestHandler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Just a regular WSGIRequestHandler except it doesn&#39;t log to the standard</span>
<span class="sd">    output any of the requests received, so as to not clutter the output for</span>
<span class="sd">    the tests&#39; results.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">log_message</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="k">pass</span>


<span class="k">class</span> <span class="nc">FSFilesHandler</span><span class="p">(</span><span class="n">WSGIHandler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    WSGI middleware that intercepts calls to a directory, as defined by one of</span>
<span class="sd">    the *_ROOT settings, and serves those files, publishing them under *_URL.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">application</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">application</span> <span class="o">=</span> <span class="n">application</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">base_url</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_base_url</span><span class="p">())</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">FSFilesHandler</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_should_handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Checks if the path should be handled. Ignores the path if:</span>

<span class="sd">        * the host is provided as part of the base_url</span>
<span class="sd">        * the request&#39;s path isn&#39;t under the media path (or equal)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">base_url</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">base_url</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">file_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the relative path to the file on disk for the given URL.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">relative_url</span> <span class="o">=</span> <span class="n">url</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">base_url</span><span class="p">[</span><span class="mi">2</span><span class="p">]):]</span>
        <span class="k">return</span> <span class="n">url2pathname</span><span class="p">(</span><span class="n">relative_url</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">Http404</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_handle</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">serve</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">Http404</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">FSFilesHandler</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">serve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="n">os_rel_path</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">file_path</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>
        <span class="n">os_rel_path</span> <span class="o">=</span> <span class="n">posixpath</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">unquote</span><span class="p">(</span><span class="n">os_rel_path</span><span class="p">))</span>
        <span class="c1"># Emulate behavior of django.contrib.staticfiles.views.serve() when it</span>
        <span class="c1"># invokes staticfiles&#39; finders functionality.</span>
        <span class="c1"># TODO: Modify if/when that internal API is refactored</span>
        <span class="n">final_rel_path</span> <span class="o">=</span> <span class="n">os_rel_path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">serve</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">final_rel_path</span><span class="p">,</span> <span class="n">document_root</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">get_base_dir</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_handle</span><span class="p">(</span><span class="n">get_path_info</span><span class="p">(</span><span class="n">environ</span><span class="p">)):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">FSFilesHandler</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__call__</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_StaticFilesHandler</span><span class="p">(</span><span class="n">FSFilesHandler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Handler for serving static files. A private class that is meant to be used</span>
<span class="sd">    solely as a convenience by LiveServerThread.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">get_base_dir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">settings</span><span class="o">.</span><span class="n">STATIC_ROOT</span>

    <span class="k">def</span> <span class="nf">get_base_url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">settings</span><span class="o">.</span><span class="n">STATIC_URL</span>


<span class="k">class</span> <span class="nc">_MediaFilesHandler</span><span class="p">(</span><span class="n">FSFilesHandler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Handler for serving the media files. A private class that is meant to be</span>
<span class="sd">    used solely as a convenience by LiveServerThread.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">get_base_dir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">settings</span><span class="o">.</span><span class="n">MEDIA_ROOT</span>

    <span class="k">def</span> <span class="nf">get_base_url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">settings</span><span class="o">.</span><span class="n">MEDIA_URL</span>


<span class="k">class</span> <span class="nc">LiveServerThread</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Thread for running a live http server while the tests are running.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">static_handler</span><span class="p">,</span> <span class="n">connections_override</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="n">host</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="n">port</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_ready</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">static_handler</span> <span class="o">=</span> <span class="n">static_handler</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connections_override</span> <span class="o">=</span> <span class="n">connections_override</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">LiveServerThread</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Sets up the live server and databases, and then loops over handling</span>
<span class="sd">        http requests.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connections_override</span><span class="p">:</span>
            <span class="c1"># Override this thread&#39;s database connections with the ones</span>
            <span class="c1"># provided by the main thread.</span>
            <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">conn</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">connections_override</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">connections</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">conn</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># Create the handler for serving static and media files</span>
            <span class="n">handler</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">static_handler</span><span class="p">(</span><span class="n">_MediaFilesHandler</span><span class="p">(</span><span class="n">WSGIHandler</span><span class="p">()))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">httpd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_server</span><span class="p">()</span>
            <span class="c1"># If binding to port zero, assign the port allocated by the OS.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">httpd</span><span class="o">.</span><span class="n">server_address</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">httpd</span><span class="o">.</span><span class="n">set_app</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_ready</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">error</span> <span class="o">=</span> <span class="n">e</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_ready</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">connections</span><span class="o">.</span><span class="n">close_all</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_create_server</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">WSGIServer</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">port</span><span class="p">),</span> <span class="n">QuietWSGIRequestHandler</span><span class="p">,</span> <span class="n">allow_reuse_address</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">terminate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;httpd&#39;</span><span class="p">):</span>
            <span class="c1"># Stop the WSGI server</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">httpd</span><span class="o">.</span><span class="n">shutdown</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">httpd</span><span class="o">.</span><span class="n">server_close</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>


<div class="viewcode-block" id="LiveServerTestCase"><a class="viewcode-back" href="../../../topics/testing/tools.html#django.test.LiveServerTestCase">[docs]</a><span class="k">class</span> <span class="nc">LiveServerTestCase</span><span class="p">(</span><span class="n">TransactionTestCase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Does basically the same as TransactionTestCase but also launches a live</span>
<span class="sd">    http server in a separate thread so that the tests may use another testing</span>
<span class="sd">    framework, such as Selenium for example, instead of the built-in dummy</span>
<span class="sd">    client.</span>
<span class="sd">    Note that it inherits from TransactionTestCase instead of TestCase because</span>
<span class="sd">    the threads do not share the same transactions (unless if using in-memory</span>
<span class="sd">    sqlite) and each thread needs to commit all their transactions so that the</span>
<span class="sd">    other thread can see the changes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">host</span> <span class="o">=</span> <span class="s1">&#39;localhost&#39;</span>
    <span class="n">port</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">server_thread_class</span> <span class="o">=</span> <span class="n">LiveServerThread</span>
    <span class="n">static_handler</span> <span class="o">=</span> <span class="n">_StaticFilesHandler</span>

    <span class="nd">@classproperty</span>
    <span class="k">def</span> <span class="nf">live_server_url</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;http://</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">host</span><span class="p">,</span> <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span><span class="o">.</span><span class="n">port</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">setUpClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">LiveServerTestCase</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">setUpClass</span><span class="p">()</span>
        <span class="n">connections_override</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">conn</span> <span class="ow">in</span> <span class="n">connections</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
            <span class="c1"># If using in-memory sqlite databases, pass the connections to</span>
            <span class="c1"># the server thread.</span>
            <span class="k">if</span> <span class="n">conn</span><span class="o">.</span><span class="n">vendor</span> <span class="o">==</span> <span class="s1">&#39;sqlite&#39;</span> <span class="ow">and</span> <span class="n">conn</span><span class="o">.</span><span class="n">is_in_memory_db</span><span class="p">():</span>
                <span class="c1"># Explicitly enable thread-shareability for this connection</span>
                <span class="n">conn</span><span class="o">.</span><span class="n">allow_thread_sharing</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">connections_override</span><span class="p">[</span><span class="n">conn</span><span class="o">.</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">conn</span>

        <span class="bp">cls</span><span class="o">.</span><span class="n">_live_server_modified_settings</span> <span class="o">=</span> <span class="n">modify_settings</span><span class="p">(</span>
            <span class="n">ALLOWED_HOSTS</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;append&#39;</span><span class="p">:</span> <span class="bp">cls</span><span class="o">.</span><span class="n">host</span><span class="p">},</span>
        <span class="p">)</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">_live_server_modified_settings</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_create_server_thread</span><span class="p">(</span><span class="n">connections_override</span><span class="p">)</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span><span class="o">.</span><span class="n">daemon</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

        <span class="c1"># Wait for the live server to be ready</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span><span class="o">.</span><span class="n">is_ready</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span><span class="o">.</span><span class="n">error</span><span class="p">:</span>
            <span class="c1"># Clean up behind ourselves, since tearDownClass won&#39;t get called in</span>
            <span class="c1"># case of errors.</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">_tearDownClassInternal</span><span class="p">()</span>
            <span class="k">raise</span> <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span><span class="o">.</span><span class="n">error</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_create_server_thread</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">connections_override</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread_class</span><span class="p">(</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">host</span><span class="p">,</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">static_handler</span><span class="p">,</span>
            <span class="n">connections_override</span><span class="o">=</span><span class="n">connections_override</span><span class="p">,</span>
            <span class="n">port</span><span class="o">=</span><span class="bp">cls</span><span class="o">.</span><span class="n">port</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_tearDownClassInternal</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="c1"># There may not be a &#39;server_thread&#39; attribute if setUpClass() for some</span>
        <span class="c1"># reasons has raised an exception.</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;server_thread&#39;</span><span class="p">):</span>
            <span class="c1"># Terminate the live server&#39;s thread</span>
            <span class="bp">cls</span><span class="o">.</span><span class="n">server_thread</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>

        <span class="c1"># Restore sqlite in-memory database connections&#39; non-shareability</span>
        <span class="k">for</span> <span class="n">conn</span> <span class="ow">in</span> <span class="n">connections</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">conn</span><span class="o">.</span><span class="n">vendor</span> <span class="o">==</span> <span class="s1">&#39;sqlite&#39;</span> <span class="ow">and</span> <span class="n">conn</span><span class="o">.</span><span class="n">is_in_memory_db</span><span class="p">():</span>
                <span class="n">conn</span><span class="o">.</span><span class="n">allow_thread_sharing</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">tearDownClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">_tearDownClassInternal</span><span class="p">()</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">_live_server_modified_settings</span><span class="o">.</span><span class="n">disable</span><span class="p">()</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">LiveServerTestCase</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">tearDownClass</span><span class="p">()</span></div>


<span class="k">class</span> <span class="nc">SerializeMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Mixin to enforce serialization of TestCases that share a common resource.</span>

<span class="sd">    Define a common &#39;lockfile&#39; for each set of TestCases to serialize. This</span>
<span class="sd">    file must exist on the filesystem.</span>

<span class="sd">    Place it early in the MRO in order to isolate setUpClass / tearDownClass.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">lockfile</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">setUpClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span><span class="o">.</span><span class="n">lockfile</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">.lockfile isn&#39;t set. Set it to a unique value &quot;</span>
                <span class="s2">&quot;in the base class.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">_lockfile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">lockfile</span><span class="p">)</span>
        <span class="n">locks</span><span class="o">.</span><span class="n">lock</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_lockfile</span><span class="p">,</span> <span class="n">locks</span><span class="o">.</span><span class="n">LOCK_EX</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SerializeMixin</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">setUpClass</span><span class="p">()</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">tearDownClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SerializeMixin</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="n">tearDownClass</span><span class="p">()</span>
        <span class="bp">cls</span><span class="o">.</span><span class="n">_lockfile</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>